﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;

namespace FileStreamsAndCrypto
{
    class Watcher
    {
        const string DIR_TO_WATCH = @"C:\Users\Academy\Desktop\NEWFOLDER";

        static void fsw_Changed(object sender, FileSystemEventArgs e)
        {
            try
            {
                using (StreamReader sr = new StreamReader(e.FullPath))
                {
                    string contents = sr.ReadToEnd();
                    Console.WriteLine(contents);
                }
            }
            catch (IOException)
            {
                Console.WriteLine("File was {0}, but I couldn't open the file", e.ChangeType);
                return;
            }
        }

        static void Main()
        {
            FileSystemWatcher fsw = new FileSystemWatcher();
            fsw.Path = DIR_TO_WATCH;
            fsw.Filter = "*.txt";
            fsw.Changed += new FileSystemEventHandler(fsw_Changed);
            fsw.EnableRaisingEvents = true;
            fsw.NotifyFilter = NotifyFilters.Size;
            Console.WriteLine("Waiting for event");
            using (ManualResetEvent are = new ManualResetEvent(false))
            {
                while (true)
                {
                    are.WaitOne();
                }
            }
        }
    }
}
